﻿Imports BUS
Imports DTO

Public Class frmLuuAcquy
    Private aqController As AcQuyBUS
    Private _aq As AcquyDTO
    Private _laq As LoaiAcquyDTO
    Private sListAQuy As New List(Of AcquyDTO)
    Private flagIsUp As Integer
    Private sListChonSua As New List(Of Integer)
    Private flagIsSuaNhieu As Boolean

    '------------------------------------------------------'
    '                 Các Hàm Xử Lý Phụ                     '
    '------------------------------------------------------'
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
    Private Sub LoadText(ByVal i As Integer)
        lbIdSelect.Text = i
        lbIdAcQuy.Text = dgvDuLieuThem.Rows(i).Cells(0).FormattedValue.ToString()
        txtTenAcQuy.Text = dgvDuLieuThem.Rows(i).Cells(1).FormattedValue.ToString()
        txtHangSX.Text = dgvDuLieuThem.Rows(i).Cells(2).FormattedValue.ToString()
        cbLoaiMach.SelectedValue = dgvDuLieuThem.Rows(i).Cells(4).FormattedValue.ToString()
        txtImp.Text = dgvDuLieuThem.Rows(i).Cells(5).FormattedValue.ToString()
        txtVmp.Text = dgvDuLieuThem.Rows(i).Cells(6).FormattedValue.ToString()
        txtVoc.Text = dgvDuLieuThem.Rows(i).Cells(7).FormattedValue.ToString()
    End Sub

    Private Sub LockText(ByVal b As Boolean)
        txtTenAcQuy.ReadOnly = b
        cbLoaiMach.Enabled = Not b
        txtHangSX.ReadOnly = b
        txtImp.ReadOnly = b
        txtVmp.ReadOnly = b
        txtVoc.ReadOnly = b
        If (b = True) Then
            txtTenAcQuy.ForeColor = Color.Black
            cbLoaiMach.ForeColor = Color.Black
            txtHangSX.ForeColor = Color.Black
            txtImp.ForeColor = Color.Black
            txtVoc.ForeColor = Color.Black
        End If
    End Sub

    Private Sub LockTextInput(ByVal b As Boolean)
        txtDiaChiFileExcel.ReadOnly = b
        btnInput.Enabled = Not b
        If b = True Then
            txtDiaChiFileExcel.ForeColor = Color.Black
        End If
    End Sub

    Private Sub BlockButton(ByVal t As Control, ByVal h As Control, ByVal v As Control, ByVal r As Control, ByVal s As Control, ByVal x As Control, Optional ByVal capdo As Byte = 0)
        Select Case capdo
            Case 0
                t.Enabled = True
                h.Enabled = True
                v.Enabled = True
                r.Enabled = True
                s.Enabled = True
                x.Enabled = True

            Case 1
                t.Enabled = True
                h.Enabled = False
                v.Enabled = False
                r.Enabled = False
                s.Enabled = False
                x.Enabled = False
        End Select
    End Sub

    Private Sub Reset()
        txtTenAcQuy.Text = ""
        txtHangSX.Text = ""
        txtVmp.Text = ""
        txtImp.Text = ""
        txtVoc.Text = ""
        cbLoaiMach.SelectedIndex = -1
    End Sub

    Private Function TestData() As Boolean
        TestData = True
        If txtTenAcQuy.Text = "" Then
            TestData = False
            MsgBox("Tên pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenAcQuy.Focus()
            Exit Function
            'ElseIf txtHangSX.Text = "" Then
            '   TestData = False
            '  MsgBox("Hãng sản xuất không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            ' txtHangSX.Focus()
            'Exit Function
        ElseIf cbLoaiMach.SelectedIndex = -1 Then
            TestData = False
            MsgBox("Loại pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            cbLoaiMach.Focus()
            Exit Function
        ElseIf txtImp.Text = "" Then
            TestData = False
            MsgBox("Dòng điện không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtImp.Focus()
            Exit Function
            'ElseIf txtVmp.Text = "" Then
            '   TestData = False
            '  MsgBox("Điện áp không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            ' txtVmp.Focus()
            'Exit Function
        ElseIf txtVoc.Text = "" Then
            TestData = False
            MsgBox("Giá không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtVoc.Focus()
            Exit Function
        End If

    End Function

    Private Function SelectNameLoai(ByVal idLoai As Integer)
        Dim sListName As List(Of LoaiAcquyDTO) = aqController.SelectNameLoai(idLoai)
        Return sListName(0).TenLoai
    End Function

    Private Sub LoadGrid()
        aqController = New AcQuyBUS()
        dgvDuLieuThem.Rows.Clear()
        For i As Integer = 0 To sListAQuy.Count - 1
            dgvDuLieuThem.Rows.Add()
            dgvDuLieuThem.Rows.Item(i).Cells(0).Value = sListAQuy(i).MaAcQuy
            dgvDuLieuThem.Rows.Item(i).Cells(1).Value = sListAQuy(i).TenAcQuy
            dgvDuLieuThem.Rows.Item(i).Cells(2).Value = sListAQuy(i).HangSanXuat
            dgvDuLieuThem.Rows.Item(i).Cells(3).Value = SelectNameLoai(Val(sListAQuy(i).MaLoai))
            dgvDuLieuThem.Rows.Item(i).Cells(4).Value = sListAQuy(i).MaLoai
            dgvDuLieuThem.Rows.Item(i).Cells(5).Value = sListAQuy(i).DongDien
            dgvDuLieuThem.Rows.Item(i).Cells(6).Value = sListAQuy(i).DienAp
            dgvDuLieuThem.Rows.Item(i).Cells(7).Value = sListAQuy(i).Gia
        Next
    End Sub

    '------------------------------------------------------'
    '                 Phần xử lý chính                    '
    '------------------------------------------------------'
    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Public Sub AddList(ByVal sListSelect As IList(Of Integer))
        For i As Integer = 0 To sListSelect.Count - 1
            aqController = New AcQuyBUS()
            Dim j As Integer = Val(sListSelect.Item(i))
            sListAQuy.AddRange(aqController.SelectById(j))
        Next
        flagIsUp = 2
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        flagIsUp = 1
        Dim MAcQuy As Integer = 0
        Dim TAcQuy As String = txtTenAcQuy.Text
        Dim MLoai As Integer = Val(cbLoaiMach.SelectedValue)
        Dim TMLoai As String = cbLoaiMach.SelectedText
        Dim HSX As String = txtHangSX.Text
        Dim DAp As Single = Val(txtVmp.Text)
        Dim DDien As Single = Val(txtImp.Text)
        Dim Gia As Integer = Val(txtVoc.Text)
        If TestData() = True Then
            _aq = New AcquyDTO(MAcQuy, TAcQuy, MLoai, HSX, DDien, DAp, Gia)
            sListAQuy.Add(_aq)
            LoadGrid()
        End If
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnLuuTatCa, 0)
    End Sub

    Private Sub frmLuuAcquy_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                'frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        cbLoaiMach.Items.Clear()
        Dim dt As New DataTable
        aqController = New AcQuyBUS()
        Dim sListlaq As New List(Of LoaiAcquyDTO)
        'aqController.LoadComboListbox(cbLoaiMach, "Select * From LOAIACQUY", "MaLoai")
        dt = aqController.LoadComboListbox()
        If dt.Rows.Count > 0 Then
            cbLoaiMach.DataSource = dt
            cbLoaiMach.ValueMember = "MaLoai"
            cbLoaiMach.DisplayMember = "TenLoai"
        End If
        cbLoaiMach.SelectedIndex = -1
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnLuuTatCa, 1)
        LockTextInput(True)
        If sListAQuy.Count > 0 Then
            LoadGrid()
            BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnLuuTatCa, 0)
        End If

    End Sub

    Private Sub cbInputFromFile_CheckedChanged(sender As Object, e As EventArgs) Handles cbInputFromFile.CheckedChanged
        If cbInputFromFile.Checked = True Then
            LockText(True)
            LockTextInput(False)
        Else
            LockText(False)
            LockTextInput(True)
        End If
    End Sub


    Private Sub dgvDuLieuThem_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
        'Dim flag = 0
        'Dim j As Integer = dgvDuLieuThem.Rows(e.RowIndex).Cells(0).Value
        'For i As Integer = 0 To sListChonSua.Count - 1
        '    If j = Val(sListChonSua(i)) Then
        '        sListChonSua.RemoveAt(i)
        '        flag = 1
        '        Exit For
        '    End If
        'Next
        'If flag = 0 Then
        '    sListChonSua.Add(j)
        'End If
        'If dgvDuLieuThem.SelectedCells.Count() = 1 Then
        '    LoadText(e.RowIndex)
        '    flagIsSuaNhieu = False
        'Else
        '    flagIsSuaNhieu = True
        '    txtTenAcQuy.Text = ""
        '    txtTenAcQuy.ReadOnly = True
        '    Reset()
        'End If
    End Sub

    Private Sub btnNhapLai_Click(sender As Object, e As EventArgs) Handles btnNhapLai.Click
        Reset()
    End Sub

    Private Sub btnXoaHet_Click(sender As Object, e As EventArgs) Handles btnXoaHet.Click
        sListAQuy.Clear()
        LoadGrid()
    End Sub

    Private Sub btnXoaMot_Click(sender As Object, e As EventArgs) Handles btnXoaMot.Click
        For i As Integer = 0 To sListAQuy.Count - 1
            If sListAQuy(i).TenAcQuy = txtTenAcQuy.Text And sListAQuy(i).MaLoai = cbLoaiMach.SelectedValue Then
                sListAQuy.RemoveAt(i)
                Exit For
            End If
        Next
        LoadGrid()
    End Sub

    Private Sub btnLuuTatCa_Click(sender As Object, e As EventArgs) Handles btnLuuTatCa.Click
        If (sListAQuy.Count > 0) Then
            For i As Integer = 0 To sListAQuy.Count - 1
                Dim listIsUp As New AcquyDTO(sListAQuy(i).MaAcQuy, sListAQuy(i).TenAcQuy, sListAQuy(i).MaLoai, sListAQuy(i).HangSanXuat, sListAQuy(i).DongDien, sListAQuy(i).DienAp, sListAQuy(i).Gia)
                aqController.InsertAcQuy(listIsUp, flagIsUp)
            Next
            MsgBox("Đã xử lý dữ liệu thành công", MsgBoxStyle.Information, "Thông báo")
            sListAQuy.Clear()
            LoadGrid()
            BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnLuuTatCa, 1)
            Reset()
            btnThoat_Click(sender, e)
        End If
    End Sub

    Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
        ofPin.Filter = "Excel (*.xls;*.xlsx)|*.xls;*.xlsx"
        ofPin.ShowDialog()

        txtDiaChiFileExcel.Text = ofPin.FileName
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & txtDiaChiFileExcel.Text & "';Extended Properties=Excel 8.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "Net-informations.com")
        DtSet = New System.Data.DataSet
        MyCommand.Fill(DtSet)
        For i As Integer = 0 To DtSet.Tables(0).Rows.Count() - 1
            Dim MAcQuy As Integer = 0
            Dim TAcQuy As String = DtSet.Tables(0).Rows(i).Item(0).ToString()
            Dim MLoai As Integer = Val(DtSet.Tables(0).Rows(i).Item(1))
            Dim HSX As String = DtSet.Tables(0).Rows(i).Item(2).ToString()
            Dim DAp As Single = Val(DtSet.Tables(0).Rows(i).Item(3))
            Dim DDien As Single = Val(DtSet.Tables(0).Rows(i).Item(4))
            Dim Gia As Integer = Val(DtSet.Tables(0).Rows(i).Item(5))
            _aq = New AcquyDTO(MAcQuy, TAcQuy, MLoai, HSX, DAp, DDien, Gia)
            sListAQuy.Add(_aq)
        Next
        LoadGrid()
        MyConnection.Close()
        flagIsUp = 1
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoaMot, btnXoaHet, btnLuuTatCa, 0)
        LockText(False)
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                If lbIdSelect.Text <> "" Then
                    Dim a As Integer = Integer.Parse(lbIdSelect.Text)

                    If sListAQuy(a).MaAcQuy = Val(lbIdAcQuy.Text) Then
                        sListAQuy(a).TenAcQuy = txtTenAcQuy.Text
                        sListAQuy(a).HangSanXuat = txtHangSX.Text
                        sListAQuy(a).MaLoai = cbLoaiMach.SelectedValue
                        sListAQuy(a).DongDien = Val(txtImp.Text)
                        sListAQuy(a).DienAp = Val(txtVmp.Text)
                        sListAQuy(a).Gia = Val(txtVoc.Text)
                    End If
                End If
            Next
        ElseIf dgvDuLieuThem.SelectedRows.Count > 1 Then
            If dgvDuLieuThem.SelectedRows(0).Cells("MaAcquy").Value.ToString() = 0 Then
                For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                    sListChonSua.Add(Integer.Parse(dgvDuLieuThem.SelectedRows(i).Cells("MaAcquy").Value.ToString()))
                    Dim j As Integer = Integer.Parse(dgvDuLieuThem.SelectedRows(i).Index)
                    If txtHangSX.Text <> "" Then
                        sListAQuy(j).HangSanXuat = txtHangSX.Text
                    End If
                    If cbLoaiMach.SelectedIndex <> -1 Then
                        sListAQuy(j).MaLoai = cbLoaiMach.SelectedValue
                    End If
                    If txtImp.Text <> "" Then
                        sListAQuy(j).DongDien = Val(txtImp.Text)
                    End If
                    If txtVmp.Text <> "" Then
                        sListAQuy(j).DienAp = Val(txtVmp.Text)
                    End If
                    If txtVoc.Text <> "" Then
                        sListAQuy(j).Gia = Val(txtVoc.Text)
                    End If
                Next
            Else
                For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                    sListChonSua.Add(Integer.Parse(dgvDuLieuThem.SelectedRows(i).Cells("MaAcquy").Value.ToString()))
                Next
                For j As Integer = 0 To sListChonSua.Count - 1
                    For i As Integer = 0 To sListAQuy.Count - 1
                        If sListChonSua(j) = sListAQuy(i).MaAcQuy Then
                            If txtHangSX.Text <> "" Then
                                sListAQuy(i).HangSanXuat = txtHangSX.Text
                            End If
                            If cbLoaiMach.SelectedIndex <> -1 Then
                                sListAQuy(i).MaLoai = cbLoaiMach.SelectedValue
                            End If
                            If txtImp.Text <> "" Then
                                sListAQuy(i).DongDien = Val(txtImp.Text)
                            End If
                            If txtVmp.Text <> "" Then
                                sListAQuy(i).DienAp = Val(txtVmp.Text)
                            End If
                            If txtVoc.Text <> "" Then
                                sListAQuy(i).Gia = Val(txtVoc.Text)
                            End If
                        End If
                    Next
                Next
            End If
            sListChonSua.Clear()
        End If
        LoadGrid()
    End Sub

    Private Sub dgvDuLieuThem_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            LoadText(dgvDuLieuThem.SelectedRows(0).Index)
            txtTenAcQuy.ReadOnly = False
        ElseIf dgvDuLieuThem.SelectedRows.Count > 1 Then
            txtTenAcQuy.Text = ""
            txtTenAcQuy.ReadOnly = True
            Reset()
        End If
    End Sub

   
End Class